<?php
include_once('JFileGlobals.php');

class JFile
{
	/**
	 * The raw text of this JFile
	 *
	 * @var String
	 */
	private $rawText;
	
	/**
	 * The JFileGlobals object that represents the globals block of this JFile
	 *
	 * @var JFileGlobals
	 */
	private $jFileGlobals;
	
	public function __construct($path)
	{
		$this->initialize($path);
	}
	
	/**
	 * Initializes all the parts of the JFile from a given path
	 *
	 * @param String $path
	 */
	private function initialize($path)
	{
		//get the contents of the file
		$this->rawText = file_get_contents($path);
		$this->jFileGlobals = new JFileGlobals($this->rawText);
	}
	
	/**
	 * Returns everything in the JFile that comes after the globals block.
	 *
	 * @return String
	 * 
	 */
	public function getFunctionText()
	{
		$endOfGlobals = strpos($this->rawText, JFileGlobals::$GLOBALS_END) + strlen(JFileGlobals::$GLOBALS_END);
		$result = substr($this->rawText, $endOfGlobals);
		
		return $result;
	}
	
	/**
	 * Creates a JFile by combining this jFile and  a given JFile
	 * 
	 * @return null
	 */
	public function absorbJFile(JFile $jFile)
	{
		
		$this->jFileGlobals->absorbGlobals($jFile->getGlobals());
		
		print_r($this->jFileGlobals);
		
		$this->rawText = $this->jFileGlobals->getRawText()."\n\n".$this->getFunctionText()."\n\n".$jFile->getFunctionText();
	}
	
	/**
	 * Gets the rawText of this JFile
	 *
	 * @return String
	 */
	public function getRawText()
	{
		return $this->rawText;
	}
	
	/**
	 * Gets this JFile's JFileGlobals object
	 *
	 * @return JFileGlobals
	 */
	public function getGlobals()
	{
		return $this->jFileGlobals;
	}
}
?>